package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.CertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditDTO;
import com.jzo2o.customer.service.ICertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 02 7月 2025 00:27
 */
@Service
public class CertificationAuditServiceImpl extends ServiceImpl<CertificationAuditMapper, WorkerCertificationAudit> implements ICertificationAuditService {
    /**
     * 分页查询服务人员认证信息
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult<WorkerCertificationAuditDTO> queryPage(WorkerCertificationAuditPageQueryReqDTO dto) {
        PageResult<WorkerCertificationAuditDTO> workerCertificationAuditPageResult = PageHelperUtils.selectPage(dto, () -> getBaseMapper().queryPage(dto));
        return workerCertificationAuditPageResult;
    }


    /**
     * 认证信息审核
     *
     * @param id
     * @param certificationStatus
     * @param rejectReason
     */
    @Override
    public void waarmerk(Long id, Integer certificationStatus, String rejectReason) {
        //健壮性判断
        if (id == null) {
            throw new BadRequestException("请选择要审核的认证信息");
        }
        if (certificationStatus == null) {
            throw new BadRequestException("请选择认证状态");
        }
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());
        //更新
        boolean update = lambdaUpdate().eq(WorkerCertificationAudit::getId, id)
                .set(WorkerCertificationAudit::getCertificationStatus, certificationStatus)
                .set(rejectReason != null && StringUtils.isNotBlank(rejectReason), WorkerCertificationAudit::getRejectReason, rejectReason)
                .set(WorkerCertificationAudit::getAuditTime, format)
                .set(WorkerCertificationAudit::getAuditStatus, 1)
                .set(WorkerCertificationAudit::getAuditorId, UserContext.currentUserId())
                .set(WorkerCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                .update();
        if (!update) {
            throw new BadRequestException("认证信息审核失败");
        }


    }
}
